import 'package:flutter/material.dart';
import 'package:flutter_deer/database/beans/get_all_goods_list.dart';
import 'package:flutter_deer/database/tables.dart';
import 'package:flutter_deer/res/colors.dart';
import 'package:flutter_deer/ui/cashier/provider/cashier_page_provider.dart';
import 'package:flutter_deer/ui/cashier/provider/goods_select_provider.dart';
import 'package:flutter_deer/util/input_formatter/mny_format.dart';
import 'package:flutter_deer/widgets/cart_item_quantity_picker.dart';
import 'package:get/get.dart';

class CashierOrderBuyItem extends StatelessWidget {
  // 控制是否展示数量选择器

  const CashierOrderBuyItem({
    super.key,
    this.itemOil,
    this.itemGoods,
  });

  final AppOrderBean? itemOil;
  final GetAllGoodsList? itemGoods;

  String getName() {
    if (itemOil != null) {
      return itemOil?.mchGoodsName ?? '';
    }
    if (itemGoods != null) {
      return itemGoods?.goodsName ?? '';
    }
    return '';
  }

  // skuName
  String getSkuName() {
    if (itemOil != null) {
      return itemOil?.goodsSkuName ?? '';
    }
    if (itemGoods != null) {
      return itemGoods?.goodsCategoryName ?? '';
    }
    return '';
  }

  @override
  Widget build(BuildContext context) {
    final mProvider = Get.find<CashierPageProvider>();
    final mSelectGoodsProvider = Get.find<GoodsSelectProvider>();
    return Container(
      decoration: BoxDecoration(
        // color: Colors.white,
        // color: Color(0xFFFF8547),
        // color: Colours.dark_app_main,
        // borderRadius: BorderRadius.only(topLeft: Radius.circular(3), topRight: Radius.circular(3)),
        border: Border(
          bottom: Divider.createBorderSide(context, width: 0.8, color: Colours.line),
        ),
      ),
      padding: const EdgeInsets.only(left: 10.0, bottom: 5.0, top: 5, right: 1.0),
      child: Row(
        children: [
          // 名称
          Expanded(
            flex: 6,
            child: Text(
              getName(),
              maxLines: 1,
              overflow: TextOverflow.ellipsis,
            ),
          ),

          // 类别
          SizedBox(
            width: 90,
            child: Align(
              child: Text(
                // '四个字题五',
                getSkuName(),
                maxLines: 1,
                overflow: TextOverflow.ellipsis,
              ),
            ),
          ),

          // 数量
          if (itemOil == null)
            SizedBox(
              width: 110,
              child: Align(
                  child: CartItemQuantityPicker(
                initialQuantity: itemGoods?.count.value ?? 1,
                onQuantityChanged: (newQuantity) {
                  print('新数量是：$newQuantity');
                  itemGoods?.count.value = newQuantity;
                  mProvider.calculateTotalOrderAmount(tag: 'count--change');
                  // 在这里更新状态或发送请求
                },
              ) // 不展示任何内容
                  ),
            )
          else
            SizedBox(
              width: 110,
              child: Align(
                  child: Text(
                // '四个字题五啊',
                '${itemOil?.vol.toYuan}L',
                style: const TextStyle(fontSize: 16),
              ) // 不展示任何内容
                  ),
            ),

          // 单价/规格
          SizedBox(
            width: 110,
            child: Align(
                child: itemOil == null
                    ? Text(
                        '${itemGoods?.price.formatYuan}元/${itemGoods?.unitName}',
                      )
                    : Column(
                        mainAxisAlignment: MainAxisAlignment.center,
                        children: [
                          Text(
                            '${itemOil?.prc.toYuan}元/${itemOil?.unitName}',
                          ),
                          if (itemOil?.formatExceptionType == 1) ...[
                            Text(
                              '平台价(${itemOil?.formatPrice})',
                              style: const TextStyle(color: Colors.red), // 设置新文本的颜色
                            ),
                          ],
                        ],
                      )),
          ),

          // 金额
          if (itemOil != null)
            SizedBox(
              width: 90,
              child: Align(
                child: Text(
                  // '四个字题五',
                  itemOil?.mny.toYuan ?? '0',
                ),
              ),
            )
          else
            Obx(() => SizedBox(
                  width: 90,
                  child: Align(
                    child: Text(
                      '${itemGoods!.price * itemGoods!.count.value}'.formatYuan,
                    ),
                  ),
                )),

          // delete
          SizedBox(
            width: 40, // 固定宽度
            height: 30, // 固定高度
            child: InkWell(
              onTap: () {
                if (itemGoods != null) {
                  mProvider.removeGoods(itemGoods!);
                  mSelectGoodsProvider.removeGoods(itemGoods!);
                }
                if (itemOil != null) {
                  mProvider.selectOilItem(itemOil!);
                }
              },
              child: const Icon(
                Icons.delete,
                color: Colors.grey,
              ),
            ),
          ),
        ],
      ),
    );
  }
}
